//
// Created by WenYF on 2016/11/3.
//

#ifndef FZJH_PACKETUTIL_H
#define FZJH_PACKETUTIL_H

#include "common.h"
#include "buffer.h"
#ifdef __cplusplus  
extern "C" {  
#endif  
enum {
    TYPE_SET = 0,
    TYPE_GET = 1,
    TYPE_RESULT = 2,
    TYPE_ERROR = 3,
    TYPE_NONE = 127
};

#define ZONE_EVENT_TYPE 0X0001
#define ROOM_EVENT_TYPE 0X0002
#define PING_EVENT_TYPE 0X0010

typedef struct ifulaid_struct {
    char *uid;
    char resource;
} ifulaid;

// for parser
// 以下char*带有length是为了再进来的数据直接解析成字符串，如果你需要。
// 如果不需要，那么你可以通过length直接读取数据，char*的长度最后有个'\0'，因此长度比length大1
typedef struct ifpacket_sessionOpen_struct {
    ifulaid *from;
    ifulaid *to;
    int id;
    byte type;
    byte protocolVersion;
    byte packetType;
    char *extra;
} ifsessionOpenPacket;

typedef struct ifpacket_sessionClose_struct {
    ifulaid *from;
    ifulaid *to;
    int id;
    byte type;
    char *extra; // 必须是utf-8的字符串
} ifsessionClosePacket;

typedef struct ifpacket_message_struct {
    ifulaid *from;
    ifulaid *to;
    int id;
    char *title; // 必须是utf-8的字符串
    char *body; // 必须是utf-8的字符串
} ifmessage;

typedef struct ifpacket_event_struct {
    ifulaid *from;
    ifulaid *to;
    int id;
    short eventType;
    byte type;
    int actionCode;
    char *body; // 必须是utf-8的字符串
} ifevent;

typedef struct ifpacket_wrapper_struct {
    byte type;
    void *packet;
} ifpacketWrapper;

// tools
// 创建一个唯一uid
ifulaid* if_create_ulaid(byte *str, int len);
ifulaid* if_create_ulaid_x(char *uid, char c);
byte* if_ulaid_toBytes(ifulaid *uid, short *length);
void if_delete_ulaid(ifulaid *uid);
// 创建会话打开话包
ifsessionOpenPacket* if_create_sessionOpen_packet(byte type);
// 会话关闭
ifsessionClosePacket* if_create_sessionClose_packet(byte type);
// 创建消息包
ifmessage* if_create_message(char *title);
// 创建事件包
ifevent* if_create_event(short eventType, byte queryType, int actionCode);
// for inner
void if_delete_packet(ifpacketWrapper *packet);
void if_delete_packet_x(void *packet, byte type);
// 得到包buffer数据
void* if_packet_getBuffer(void *packetWrapper, byte type, byte key);
#ifdef __cplusplus  
}  
#endif 
#endif //FZJH_PACKETUTIL_H
